﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace FX.CC.Andon.EqKanban
{
    public partial class frmModelInfo : DevExpress.XtraEditors.XtraForm
    {
        System.Timers.Timer tmr;
        Action<string, Color> actUpdateTime;

        ModelInfoWrapper modelInfoObj;
        public frmModelInfo()
        {
            InitializeComponent();
            lbEqName.Text = Program.bindingEq.EqName;
            actUpdateTime = new Action<string, Color>((str, clr) =>{
                lbRealTime.Text = str;
                lbRealTime.ForeColor = clr;
            });
        }


        public void SetContent(ModelInfoWrapper obj) {
            //txtEqName.Text = obj.eqn;
            lbModelMin.Text = obj.ModelMin.ToString("0.##") + "分钟";
            lbStartTime.Text = obj.StartTime.ToString("HH:mm:ss");

            this.modelInfoObj = obj;

            tmr = new System.Timers.Timer(1000);
            tmr.Elapsed += new System.Timers.ElapsedEventHandler(tmr_Elapsed);
        }
        
        void tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            var stam = DateTime.Now.Subtract(this.modelInfoObj.StartTime).TotalMinutes;
            try
            {
                this.Invoke(actUpdateTime, stam.ToString("0.00分钟"), stam < modelInfoObj.ModelMin ? Color.Green : Color.Red);
            }
            catch { }
        }

        private void frmModelInfo_Shown(object sender, EventArgs e)
        {
            tmr.Start();
        }

        private void frmModelInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            tmr.Stop();
        }
    }
}